﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALCompanyPartner
    /// </summary>
    public partial class DALCompanyPartner
    {
        public DALCompanyPartner()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbCompanyPartner");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbCompanyPartner model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbCompanyPartner(");
            strSql.Append("companyName,linker,linkWay,description,isProcess,partnerWay,email,remark,processRemark,deleted,addTime,processTime,processAdmin,goodsType,suggeust,isCertificate,companyAddress)");
            strSql.Append(" values (");
            strSql.Append("@companyName,@linker,@linkWay,@description,@isProcess,@partnerWay,@email,@remark,@processRemark,@deleted,@addTime,@processTime,@processAdmin,@goodsType,@suggeust,@isCertificate,@companyAddress)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@companyName", SqlDbType.NVarChar,500),
					new SqlParameter("@linker", SqlDbType.NVarChar,500),
					new SqlParameter("@linkWay", SqlDbType.NVarChar,500),
					new SqlParameter("@description", SqlDbType.NVarChar,2000),
					new SqlParameter("@isProcess", SqlDbType.Bit,1),
					new SqlParameter("@partnerWay", SqlDbType.NVarChar,200),
					new SqlParameter("@email", SqlDbType.NVarChar,200),
					new SqlParameter("@remark", SqlDbType.NVarChar,2000),
					new SqlParameter("@processRemark", SqlDbType.NVarChar,500),
					new SqlParameter("@deleted", SqlDbType.Bit,1),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@processTime", SqlDbType.DateTime),
					new SqlParameter("@processAdmin", SqlDbType.Int,4),
					new SqlParameter("@goodsType", SqlDbType.NVarChar,500),
					new SqlParameter("@suggeust", SqlDbType.NVarChar,2000),
					new SqlParameter("@isCertificate", SqlDbType.Bit,1),
					new SqlParameter("@companyAddress", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.companyName;
            parameters[1].Value = model.linker;
            parameters[2].Value = model.linkWay;
            parameters[3].Value = model.description;
            parameters[4].Value = model.isProcess;
            parameters[5].Value = model.partnerWay;
            parameters[6].Value = model.email;
            parameters[7].Value = model.remark;
            parameters[8].Value = model.processRemark;
            parameters[9].Value = model.deleted;
            parameters[10].Value = model.addTime;
            parameters[11].Value = model.processTime;
            parameters[12].Value = model.processAdmin;
            parameters[13].Value = model.goodsType;
            parameters[14].Value = model.suggeust;
            parameters[15].Value = model.isCertificate;
            parameters[16].Value = model.companyAddress;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbCompanyPartner model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbCompanyPartner set ");
            strSql.Append("companyName=@companyName,");
            strSql.Append("linker=@linker,");
            strSql.Append("linkWay=@linkWay,");
            strSql.Append("description=@description,");
            strSql.Append("isProcess=@isProcess,");
            strSql.Append("partnerWay=@partnerWay,");
            strSql.Append("email=@email,");
            strSql.Append("remark=@remark,");
            strSql.Append("processRemark=@processRemark,");
            strSql.Append("deleted=@deleted,");
            strSql.Append("addTime=@addTime,");
            strSql.Append("processTime=@processTime,");
            strSql.Append("processAdmin=@processAdmin,");
            strSql.Append("goodsType=@goodsType,");
            strSql.Append("suggeust=@suggeust,");
            strSql.Append("isCertificate=@isCertificate,");
            strSql.Append("companyAddress=@companyAddress");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@companyName", SqlDbType.NVarChar,500),
					new SqlParameter("@linker", SqlDbType.NVarChar,500),
					new SqlParameter("@linkWay", SqlDbType.NVarChar,500),
					new SqlParameter("@description", SqlDbType.NVarChar,2000),
					new SqlParameter("@isProcess", SqlDbType.Bit,1),
					new SqlParameter("@partnerWay", SqlDbType.NVarChar,200),
					new SqlParameter("@email", SqlDbType.NVarChar,200),
					new SqlParameter("@remark", SqlDbType.NVarChar,2000),
					new SqlParameter("@processRemark", SqlDbType.NVarChar,500),
					new SqlParameter("@deleted", SqlDbType.Bit,1),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@processTime", SqlDbType.DateTime),
					new SqlParameter("@processAdmin", SqlDbType.Int,4),
					new SqlParameter("@goodsType", SqlDbType.NVarChar,500),
					new SqlParameter("@suggeust", SqlDbType.NVarChar,2000),
					new SqlParameter("@isCertificate", SqlDbType.Bit,1),
					new SqlParameter("@companyAddress", SqlDbType.NVarChar,500),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.companyName;
            parameters[1].Value = model.linker;
            parameters[2].Value = model.linkWay;
            parameters[3].Value = model.description;
            parameters[4].Value = model.isProcess;
            parameters[5].Value = model.partnerWay;
            parameters[6].Value = model.email;
            parameters[7].Value = model.remark;
            parameters[8].Value = model.processRemark;
            parameters[9].Value = model.deleted;
            parameters[10].Value = model.addTime;
            parameters[11].Value = model.processTime;
            parameters[12].Value = model.processAdmin;
            parameters[13].Value = model.goodsType;
            parameters[14].Value = model.suggeust;
            parameters[15].Value = model.isCertificate;
            parameters[16].Value = model.companyAddress;
            parameters[17].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbCompanyPartner ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbCompanyPartner ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbCompanyPartner GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,companyName,linker,linkWay,description,isProcess,partnerWay,email,remark,processRemark,deleted,addTime,processTime,processAdmin,goodsType,suggeust,isCertificate,companyAddress from tbCompanyPartner ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            Gift.Model.tbCompanyPartner model = new Gift.Model.tbCompanyPartner();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["companyName"] != null && ds.Tables[0].Rows[0]["companyName"].ToString() != "")
                {
                    model.companyName = ds.Tables[0].Rows[0]["companyName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["linker"] != null && ds.Tables[0].Rows[0]["linker"].ToString() != "")
                {
                    model.linker = ds.Tables[0].Rows[0]["linker"].ToString();
                }
                if (ds.Tables[0].Rows[0]["linkWay"] != null && ds.Tables[0].Rows[0]["linkWay"].ToString() != "")
                {
                    model.linkWay = ds.Tables[0].Rows[0]["linkWay"].ToString();
                }
                if (ds.Tables[0].Rows[0]["description"] != null && ds.Tables[0].Rows[0]["description"].ToString() != "")
                {
                    model.description = ds.Tables[0].Rows[0]["description"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isProcess"] != null && ds.Tables[0].Rows[0]["isProcess"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isProcess"].ToString() == "1") || (ds.Tables[0].Rows[0]["isProcess"].ToString().ToLower() == "true"))
                    {
                        model.isProcess = true;
                    }
                    else
                    {
                        model.isProcess = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["partnerWay"] != null && ds.Tables[0].Rows[0]["partnerWay"].ToString() != "")
                {
                    model.partnerWay = ds.Tables[0].Rows[0]["partnerWay"].ToString();
                }
                if (ds.Tables[0].Rows[0]["email"] != null && ds.Tables[0].Rows[0]["email"].ToString() != "")
                {
                    model.email = ds.Tables[0].Rows[0]["email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["remark"] != null && ds.Tables[0].Rows[0]["remark"].ToString() != "")
                {
                    model.remark = ds.Tables[0].Rows[0]["remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["processRemark"] != null && ds.Tables[0].Rows[0]["processRemark"].ToString() != "")
                {
                    model.processRemark = ds.Tables[0].Rows[0]["processRemark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["deleted"] != null && ds.Tables[0].Rows[0]["deleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["deleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["deleted"].ToString().ToLower() == "true"))
                    {
                        model.deleted = true;
                    }
                    else
                    {
                        model.deleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["addTime"] != null && ds.Tables[0].Rows[0]["addTime"].ToString() != "")
                {
                    model.addTime = DateTime.Parse(ds.Tables[0].Rows[0]["addTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["processTime"] != null && ds.Tables[0].Rows[0]["processTime"].ToString() != "")
                {
                    model.processTime = DateTime.Parse(ds.Tables[0].Rows[0]["processTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["processAdmin"] != null && ds.Tables[0].Rows[0]["processAdmin"].ToString() != "")
                {
                    model.processAdmin = int.Parse(ds.Tables[0].Rows[0]["processAdmin"].ToString());
                }
                if (ds.Tables[0].Rows[0]["goodsType"] != null && ds.Tables[0].Rows[0]["goodsType"].ToString() != "")
                {
                    model.goodsType = ds.Tables[0].Rows[0]["goodsType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["suggeust"] != null && ds.Tables[0].Rows[0]["suggeust"].ToString() != "")
                {
                    model.suggeust = ds.Tables[0].Rows[0]["suggeust"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isCertificate"] != null && ds.Tables[0].Rows[0]["isCertificate"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isCertificate"].ToString() == "1") || (ds.Tables[0].Rows[0]["isCertificate"].ToString().ToLower() == "true"))
                    {
                        model.isCertificate = true;
                    }
                    else
                    {
                        model.isCertificate = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["companyAddress"] != null && ds.Tables[0].Rows[0]["companyAddress"].ToString() != "")
                {
                    model.companyAddress = ds.Tables[0].Rows[0]["companyAddress"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,companyName,linker,linkWay,description,isProcess,partnerWay,email,remark,processRemark,deleted,addTime,processTime,processAdmin,goodsType,suggeust,isCertificate,companyAddress ");
            strSql.Append(" FROM tbCompanyPartner ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,companyName,linker,linkWay,description,isProcess,partnerWay,email,remark,processRemark,deleted,addTime,processTime,processAdmin,goodsType,suggeust,isCertificate,companyAddress ");
            strSql.Append(" FROM tbCompanyPartner ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbCompanyPartner ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from tbCompanyPartner T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbCompanyPartner";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

